package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"yun_image/config/Juhe"
	"yun_image/config/api"
	"yun_image/config/image"
	"yun_image/entity"
	"yun_image/service"
)

const (
	getUpLoadPassPath       = api.GetUpLoadPassPath
	saveImgPath             = api.SaveImgPath
	getImgsByAccountPath    = api.GetImgsByAccountPath
	getImgsByCategoryIdPath = api.GetImgsByCategoryIdPath
	removeImgsPath          = api.RemoveImgsPath
)

func getUpLoadPass(context *gin.Context) {
	id, ts, sign := Juhe.GetIdTsSign()
	context.JSON(http.StatusOK, gin.H{
		"id":   id,
		"ts":   ts,
		"sign": sign,
	})
}
func saveImg(context *gin.Context) {
	var user entity.User
	userVal, _ := context.Get("user")
	user, _ = userVal.(entity.User)
	if user.Number >= user.MaxNumber {
		context.JSON(http.StatusOK, gin.H{
			"overMaxNumber": true,
		})
		return
	}
	token := context.GetHeader("token")
	url := context.PostForm("url")
	yunId := context.PostForm("yunId")
	categoryIdStr := context.PostForm("categoryId")
	categoryId := image.DefaultCategoryId
	if categoryIdStr != "" {
		var err1 error
		categoryId, err1 = strconv.Atoi(categoryIdStr)
		if err1 != nil {
			context.String(http.StatusInternalServerError, "")
			return
		}
	}
	if url == "" || yunId == "" {
		context.String(http.StatusInternalServerError, "")
		return
	}
	var img entity.UserImg
	err := service.SaveImg(&img, token, url, yunId, categoryId)
	if err != nil {
		context.String(http.StatusInternalServerError, "")
		return
	}
	context.JSON(http.StatusOK, img)
}

func getImgsByAccount(context *gin.Context) {
	token := context.GetHeader("token")
	var imgs = make([]entity.UserImg, 0)
	err := service.GetImgsByToken(&imgs, token)
	if err != nil {
		context.String(http.StatusInternalServerError, "")
		return
	}
	context.JSON(http.StatusOK, imgs)
}

func getImgsByCategoryId(context *gin.Context) {
	categoryId, err := strconv.Atoi(context.PostForm("categoryId"))
	if err != nil {
		context.String(http.StatusInternalServerError, "")
		return
	}
	var imgs = make([]entity.UserImg, 0)
	err = service.GetImgsByCategoryId(&imgs, categoryId)
	if err != nil {
		context.String(http.StatusInternalServerError, "")
		return
	}
	context.JSON(http.StatusOK, imgs)
}

func removeImgs(context *gin.Context) {
	var user entity.User
	userVal, _ := context.Get("user")
	user, _ = userVal.(entity.User)

	idsStr, ok := context.GetPostFormArray("ids[]")
	if !ok {
		context.String(http.StatusInternalServerError, "")
		return
	}
	var ids = make([]int, len(idsStr))
	for index, idStr := range idsStr {
		id, err := strconv.Atoi(idStr)
		if err != nil {
			context.String(http.StatusInternalServerError, "")
			return
		}
		ids[index] = id
	}
	err := service.RemoveImgs(ids, user.Account)
	if err != nil {
		context.String(http.StatusInternalServerError, "")
		return
	} else {
		context.JSON(http.StatusOK, gin.H{
			"success": true,
		})
	}
}

func updateImgCategoryId(context *gin.Context) {
	userVal, _ := context.Get("user")
	user, _ := userVal.(entity.User)
	imgIdStr := context.PostForm("imgId")
	categoryIdStr := context.PostForm("categoryId")
	imgId, err1 := strconv.Atoi(imgIdStr)
	categoryId, err2 := strconv.Atoi(categoryIdStr)
	if err1 != nil || err2 != nil {
		context.String(http.StatusInternalServerError, "")
		return
	}
	err3 := service.UpdateImgCategoryId(imgId, categoryId, user.Account)
	if err3 != nil {
		context.String(http.StatusInternalServerError, "")
		return
	}
	context.JSON(http.StatusOK, gin.H{
		"success": true,
	})
}

func getImageShareList(context *gin.Context) {
	var imageShareList []entity.ImageShare
	categoryIdStr := context.PostForm("categoryId")
	categoryId, err1 := strconv.Atoi(categoryIdStr)
	if err1 != nil {
		context.String(http.StatusInternalServerError, "")
		return
	}
	err2 := service.GetImageShareList(&imageShareList, categoryId)
	if err2 != nil {
		context.String(http.StatusInternalServerError, "")
		return
	}
	context.JSON(http.StatusOK, imageShareList)
}
func removeImageShare(context *gin.Context) {
	userVal, _ := context.Get("user")
	user, _ := userVal.(entity.User)
	id := context.PostForm("id")
	if id == "" {
		context.String(http.StatusInternalServerError, "")
		return
	}
	err := service.RemoveImageShare(id, user.Account)
	if err != nil {
		context.String(http.StatusInternalServerError, "")
		return
	}
	context.JSON(http.StatusOK, gin.H{
		"success": true,
	})
}
func saveImageShare(context *gin.Context) {
	userVal, _ := context.Get("user")
	user, _ := userVal.(entity.User)
	account := user.Account
	categoryIdStr := context.PostForm("categoryId")
	categoryId, err1 := strconv.Atoi(categoryIdStr)
	if err1 != nil {
		context.String(http.StatusInternalServerError, "")
		return
	}
	var imageShare entity.ImageShare
	err2 := service.SaveImageShare(&imageShare, account, categoryId)
	if err2 != nil {
		context.String(http.StatusInternalServerError, "")
		return
	}
	context.JSON(http.StatusOK, imageShare)
}
func getImgsByShareId(context *gin.Context) {
	id := context.PostForm("id")
	if id == "" {
		context.String(http.StatusInternalServerError, "")
		return
	}
	var imgs []entity.UserImg
	err := service.GetImgsByShareId(&imgs, id)
	if err != nil {
		context.String(http.StatusInternalServerError, "")
		return
	}
	context.JSON(http.StatusOK, imgs)
}
func startImgApi() {
	router.POST(getUpLoadPassPath, getUpLoadPass)
	router.POST(saveImgPath, saveImg)
	router.POST(getImgsByAccountPath, getImgsByAccount)
	router.POST(getImgsByCategoryIdPath, getImgsByCategoryId)
	router.POST(removeImgsPath, removeImgs)
	router.POST(api.GetImageShareListPath, getImageShareList)
	router.POST(api.SaveImageSharePath, saveImageShare)
	router.POST(api.RemoveImageSharePath, removeImageShare)
	router.POST(api.UpdateImgCategoryIdPath, updateImgCategoryId)
	router.POST(api.GetImgsByShareIdPath, getImgsByShareId)
}
